package com.mrobuy.jdvc.dao.impl;

import com.gavel.database.SQLExecutor;
import com.gavel.utils.StringUtils;
import com.mrobuy.jdvc.Result;
import com.mrobuy.jdvc.dao.VCBrandDao;
import com.mrobuy.jdvc.dao.VCProductInfoDao;
import com.mrobuy.jdvc.entity.VCBrand;
import com.mrobuy.jdvc.entity.VCCateBrand;
import com.mrobuy.jdvc.entity.VCProductInfo;
import com.mrobuy.util.MD5Utils;
import com.mrobuy.util.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Repository("vcProductInfoDao")
public class VCProductInfoDaoImpl  extends  CommonDaoImpl implements VCProductInfoDao {

    private static Logger LOG = LoggerFactory.getLogger(VCProductInfoDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<VCProductInfo> list() {

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_PRODUCT_INFO");
        try {
            List<VCProductInfo> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCProductInfo.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Pagination load(int start, int length, VCProductInfo condition) {
        Pagination pagination = new Pagination();

        List<Object> params = new ArrayList<>();
        StringBuilder whereCond = new StringBuilder(" where 1 =1 ");

        if ( StringUtils.isNotBlank(condition.getWareId()) ) {
            whereCond.append(" and WAREID = ? ");
            params.add(condition.getWareId().trim() );
        }

        if ( StringUtils.isNotBlank(condition.getWareName()) ) {
            whereCond.append("  and WARENAME like ? ");
            params.add("%" + condition.getWareName().trim() + "%");
        }

        if ( condition.getSaleState()!=null ) {
            whereCond.append(" and SALESTATE = ? ");
            params.add(condition.getSaleState() );
        }

        if ( condition.getIsPrimary()!=null ) {
            whereCond.append(" and ISPRIMARY = ? ");
            params.add(condition.getIsPrimary() );
        }



        try {
            int count = jdbcTemplate.queryForObject("select count(*) from VC_PRODUCT_INFO " + whereCond.toString(), params.toArray(new Object[params.size()]), Integer.class);

            params.add(start);
            params.add(length);

            List<VCProductInfo> list = jdbcTemplate.query("select * from VC_PRODUCT_INFO " + whereCond.toString() + " limit ?, ?", params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(VCProductInfo.class));

            pagination.setRecordsTotal(count);
            pagination.setRecordsFiltered(count);
            pagination.setData(list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return pagination;
    }

    @Override
    public VCProductInfo load(VCProductInfo condition) {
        if ( condition==null || StringUtils.isBlank(condition.getWareId())) {
            return null;
        }

        try {
            return (VCProductInfo)jdbcTemplate.queryForObject("select * from VC_PRODUCT_INFO where WAREID = ?", new Object[]{condition.getWareId()}, new BeanPropertyRowMapper(VCProductInfo.class));
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }

    @Override
    public VCProductInfo load(String wareId) {
        if ( StringUtils.isBlank(wareId)) {
            return null;
        }

        try {
            return (VCProductInfo)jdbcTemplate.queryForObject("select * from VC_PRODUCT_INFO where WAREID = ?", new Object[]{ wareId }, new BeanPropertyRowMapper(VCProductInfo.class));
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }

    @Override
    public List<VCProductInfo> list(String vendorCode, Integer isPrimary) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_PRODUCT_INFO where VENDORCODE = ? and  ISPRIMARY = ?");
        try {
            List<VCProductInfo> list = jdbcTemplate.query(sql.toString(), new Object[]{vendorCode, isPrimary},  new BeanPropertyRowMapper(VCProductInfo.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }
}
